<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <!--搜索-->
          <SearchUiNew style="margin-top: 10px" :searchFormArr="searchFormArr" @searchParma="searchParma"></SearchUiNew>
        </div>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <div class="img-list">
          <div class="img-box" v-for="(item, index) in dataList" :key="item.id + index">
            <img :src="item.image" @click="handleImgClick(item)" />
          </div>
        </div>
        <el-empty v-if="dataList.length === 0"></el-empty>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
        <div class="panel-line" style="margin-top: 20px">
          <el-button type="danger" @click="handleDeleteAll" size="mini" :disabled="dataList.length === 0">整页已读</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { apiSysZcImageList, apiSysZcImageRead } from '@/api/assetsPublic'

export default {
  name: 'AssetIdentifyImage',
  components: {},
  data() {
    return {
      searchFormArr: [
        {
          fieldName: '发布时间:',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择发布时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
        {
          fieldName: '所属地区:',
          type: 'cascader',
          field: 'zone',
          hidden: false,
          props: { value: 'code', label: 'name', checkStrictly: true, multiple: 'true' },
          option: [],
        },
      ],
      searchParams: {
        pub_start_time: '',
        pub_end_time: '',
        sheng_id: [],
        shi_id: [],
        zone_id: [],
      },
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
    }
  },
  activated() {},
  mounted() {
    this.getSelectList()
    this.getDataList()
  },
  destroyed() {},
  methods: {
    // 获取下拉数据
    getSelectList() {
      this.$store.dispatch('select/getCityList').then((res) => {
        const formItem = this.searchFormArr.find((item) => item.field === 'zone')
        this.areaList = formItem.option = [
          {
            code: -1,
            name: '未分配地区',
          },
          {
            code: -2,
            name: '全国',
          },
          ...res,
        ]
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let info = { pageNumber: this.pageNumber, pageSize: this.pageSize }
      info = Object.assign(info, this.searchParams)
      apiSysZcImageList(info)
        .then((res) => {
          const { data, total, others } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      let list = ['time', 'zone']
      this.searchFormArr.forEach((item) => {
        if (list.includes(item.field)) return

        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })

      if (e.time) {
        this.searchParams.pub_start_time = e.time[0]
        this.searchParams.pub_end_time = e.time[1]
      } else {
        this.searchParams.pub_start_time = this.searchParams.pub_end_time = ''
      }
      if (e.zone) {
        this.areaDefault = e.zone
        this.searchParams.sheng_id = []
        this.searchParams.shi_id = []
        this.searchParams.zone_id = []
        this.searchParams.is_area_choice = 0
        let all = false
        e.zone.forEach((item) => {
          if (item[0] === -2) {
            // 全国
            all = true
          } else if (item[0] === -1) {
            // 未分配地区
            this.searchParams.is_area_choice = 1
          } else {
            const length = item.length
            if (length === 1) {
              this.searchParams.sheng_id.push(item[0])
            } else if (length === 2) {
              this.searchParams.shi_id.push(item[1])
            } else if (length === 3) {
              this.searchParams.zone_id.push(item[2])
            }
          }
        })
        if (all) {
          this.searchParams.sheng_id = []
          this.searchParams.shi_id = []
          this.searchParams.zone_id = []
          this.searchParams.is_area_choice = 0
        }
      } else {
        this.searchParams.sheng_id = this.searchParams.shi_id = this.searchParams.zone_id = []
      }
      this.pageNumber = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 整页已读
    handleDeleteAll() {
      apiSysZcImageRead({ ids: this.dataList.map((item) => item.id) }).then((res) => {
        this.$message.success('操作成功')
        this.pageNumber > 1 && this.pageNumber--
        this.getDataList()
      })
    },
    handleImgClick(item) {
      window.open(`${location.origin}/#/assetsPublic/AssetPublish?id=${item.id}`)
    },
  },
}
</script>

<style lang="scss" scoped>
.img-list {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  flex-wrap: wrap;
}
.img-box {
  width: 120px;
  height: 120px;
  margin: 4px;
  border: 1px solid #ddd;
  border-radius: 4px;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    cursor: pointer;
  }
}
</style>
